import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';
import {
    Message,
    Notification,
    MessageBox
} from 'element-ui'

Vue.use(Vuex);

const store = new Vuex.Store({
    // 定义状态
    state: {
        ajaxApi: '/',
        unstrapi: '/api',
        unstrapiPro: 'http://192.168.2.179:9880', //打包 ---
        // unstrapiPro: 'http://117.50.65.85:9880',
        wsApi: 'ws://' + location.host + ':' + location.port + '/',
        ajaxApiSimplypre: '/apis',
        // ajaxApiSimply: 'http://117.50.65.85:9881',
        ajaxApiSimply: 'http://192.168.2.179:9880', //打包 ---
        // ajaxApiSimply: 'http://192.168.1.179:9881',//
        ajaxSetPre: '/pre',
        // ajaxSetbac: 'http://117.50.65.85:9880',
        // ajaxApi: 'http://192.168.1.150:9000/',
        // wsApi: 'ws://192.168.1.150:9000/',
        env: process.env.NODE_ENV,
        baseUrl: process.env.BASE_URL,
        unstructureData: {}, //非结构化数据
    },
    actions: {
        // 封装一个 ajax 方法--打包不用添加多余
        ajaxSetting(context, {
            url,
            data,
            method,
            succeed
        }) {
            if (url.substr(0, 6) == 'local:') {
                url = (context.state.env === 'production' ? './' : '') + url.substr(6);
            } else if (url.substr(0, 5) == "http:") {
                url;
            } else if (context.state.env === 'production') {
                // } else if (url.substr(0, 5) == 'http:') {
                url;
                // url =context.state.unstrapiPro+ url;
            } else {
                // url = context.state.ajaxApi + url;//old
                url = context.state.ajaxSetPre + url;
            }
            axios({
                url: url,
                method: method || 'get',
                params: (method || 'get') == 'get' ? data : {},
                headers: {
                    'token': sessionStorage.token
                },
                data: data || {}
            }).then(function (res) {
                console.log(res)
                if (res.data.code && res.data.code.toString() !== '200') {
                    Message({
                        type: 'warning',
                        showClose: true,
                        message: res.data.message,
                    });
                    return false;
                }

                if (succeed && typeof succeed == 'function') {
                    succeed(res.data);
                }
            }).catch((error) => {
                console.log(error);
                console.log(error.response);

                if (error.response) {
                    const status = error.response.status;
                    if (status === 401) {
                        window.location.href = context.state.env === 'production' ? './#/login' : '/#/login';
                    } else if (status === 402) {
                        const h = this._vm.$createElement;
                        Notification({
                            title: '授权提醒',
                            duration: 0,
                            dangerouslyUseHTMLString: true,
                            message: h('span', null, [
                                h('span', null, '授权已过期，请重新'),
                                h('a', {
                                    style: 'color: #3579f8',
                                    attrs: {
                                        href: 'javascript:;'
                                    },
                                    on: {
                                        click: this._actions.authImport
                                    }
                                }, '导入授权')
                            ])
                        });
                    }
                } else {
                    Message({
                        type: 'error',
                        showClose: true,
                        message: '请求失败'
                    });
                }
            });
        },
        // 封装一个 ajax 方法
        ajax(context, {
            url,
            data,
            method,
            succeed
        }) {
            if (url.substr(0, 6) == 'local:') {
                url = (context.state.env === 'production' ? './' : '') + url.substr(6);
            } else if (url.substr(0, 5) == "http:") {
                url;
            } else if (context.state.env === 'production') {
                // } else if (url.substr(0, 5) == 'http:') {
                // url = context.state.unstrapiPro + url;
                url;
            } else {
                // url = context.state.ajaxApi + url;//old
                url;
                // url = context.state.unstrapi + url;
            }

            axios({
                url: url,
                method: method || 'get',
                params: (method || 'get') == 'get' ? data : {},
                headers: {
                    'token': sessionStorage.token
                },
                data: data || {}
            }).then(function (res) {
                console.log(res)
                if (res.data.code && res.data.code.toString() !== '200') {
                    Message({
                        type: 'warning',
                        showClose: true,
                        message: res.data.message,
                    });
                    return false;
                }

                if (succeed && typeof succeed == 'function') {
                    succeed(res.data);
                }
            }).catch((error) => {
                console.log(error);

                if (error.response) {
                    const status = error.response.status;
                    if (status === 401) {
                        window.location.href = context.state.env === 'production' ? './#/login' : '/#/login';
                    } else if (status === 402) {
                        const h = this._vm.$createElement;
                        Notification({
                            title: '授权提醒',
                            duration: 0,
                            dangerouslyUseHTMLString: true,
                            message: h('span', null, [
                                h('span', null, '授权已过期，请重新'),
                                h('a', {
                                    style: 'color: #3579f8',
                                    attrs: {
                                        href: 'javascript:;'
                                    },
                                    on: {
                                        click: this._actions.authImport
                                    }
                                }, '导入授权')
                            ])
                        });
                    }
                } else {
                    Message({
                        type: 'error',
                        showClose: true,
                        message: '请求失败'
                    });
                }
            });
        },
        // 二个 ajax 方法 apis
        ajaxDb(context, {
            url,
            data,
            method,
            succeed
        }) {
            if (url.substr(0, 6) == 'local:') {
                url = (context.state.env === 'production' ? './' : '') + url.substr(6);
                // } else if (url.substr(0, 5) == 'http:') {
            } else if (url.substr(0, 5) == "http:") {
                url;
            } else if (context.state.env === 'production') {
                // url = context.state.ajaxApiSimply + url;
                url;
            } else {
                url = context.state.ajaxApiSimplypre + url;
            }

            axios({
                url: url,
                method: method || 'get',
                params: (method || 'get') == 'get' ? data : {},
                headers: {
                    'token': sessionStorage.token
                },
                data: data || {}
            }).then(function (res) {
                console.log(res)
                if (res.data.code && res.data.code.toString() !== '200') {
                    Message({
                        type: 'warning',
                        showClose: true,
                        message: res.data.message,
                    });
                    return false;
                }

                if (succeed && typeof succeed == 'function') {
                    succeed(res.data);
                }
            }).catch((error) => {
                console.log(error);

                if (error.response) {
                    const status = error.response.status;
                    if (status === 401) {
                        window.location.href = context.state.env === 'production' ? './#/login' : '/#/login';
                    } else if (status === 402) {
                        const h = this._vm.$createElement;
                        Notification({
                            title: '授权提醒',
                            duration: 0,
                            dangerouslyUseHTMLString: true,
                            message: h('span', null, [
                                h('span', null, '授权已过期，请重新'),
                                h('a', {
                                    style: 'color: #3579f8',
                                    attrs: {
                                        href: 'javascript:;'
                                    },
                                    on: {
                                        click: this._actions.authImport
                                    }
                                }, '导入授权')
                            ])
                        });
                    }
                } else {
                    Message({
                        type: 'error',
                        showClose: true,
                        message: '请求失败'
                    });
                }
            });
        },
        // 封装三个 ajax 方法
        ajaxSimply(context, {
            url,
            data,
            method,
            succeed
        }) {
            if (url.substr(0, 6) == 'local:') {
                url = (context.state.env === 'production' ? './' : '') + url.substr(6);
                // } else if (url.substr(0, 5) == 'http:') {
            } else if (url.substr(0, 5) == "http:") {
                url;
            } else if (context.state.env === 'production') {
                // url = context.state.ajaxApiSimply + url;
                url;
            } else {
                url = context.state.ajaxApiSimplypre + url;
            }

            axios({
                url: url,
                method: method || 'get',
                params: (method || 'get') == 'get' ? data : {},
                headers: {
                    'token': sessionStorage.token
                },
                data: data || {}
            }).then(function (res) {
                console.log(res)
                if (res.data.code && res.data.code.toString() !== '200') {
                    Message({
                        type: 'warning',
                        showClose: true,
                        message: res.data.message,
                    });
                    return false;
                }

                if (succeed && typeof succeed == 'function') {
                    succeed(res);
                }
            }).catch((error) => {
                console.log(error);

                if (error.response) {
                    const status = error.response.status;
                    if (status === 401) {
                        window.location.href = context.state.env === 'production' ? './#/login' : '/#/login';
                    } else if (status === 402) {
                        const h = this._vm.$createElement;
                        Notification({
                            title: '授权提醒',
                            duration: 0,
                            dangerouslyUseHTMLString: true,
                            message: h('span', null, [
                                h('span', null, '授权已过期，请重新'),
                                h('a', {
                                    style: 'color: #3579f8',
                                    attrs: {
                                        href: 'javascript:;'
                                    },
                                    on: {
                                        click: this._actions.authImport
                                    }
                                }, '导入授权')
                            ])
                        });
                    }
                } else {
                    Message({
                        type: 'error',
                        showClose: true,
                        message: '请求失败'
                    });
                }
            });
        },
                // 封装一个 ajax 方法---api
                ajaxApi(context, {
                    url,
                    data,
                    method,
                    succeed
                }) {
                    if (context.state.env === 'production') {
                        url;
                    } else {
                        url = context.state.unstrapi + url;
                    }
        
        
                    axios({
                        url: url,
                        method: method || 'get',
                        params: (method || 'get') == 'get' ? data : {},
                        headers: {
                            'token': sessionStorage.token
                        },
                        data: data || {}
                    }).then(function (res) {
                        console.log(res)
                        if (res.data.code && res.data.code.toString() !== '200') {
                            Message({
                                type: 'warning',
                                showClose: true,
                                message: res.data.message,
                            });
                            return false;
                        }
        
                        if (succeed && typeof succeed == 'function') {
                            succeed(res.data);
                        }
                    }).catch((error) => {
                        console.log(error);
        
                        if (error.response) {
                            const status = error.response.status;
                            if (status === 401) {
                                window.location.href = context.state.env === 'production' ? './#/login' : '/#/login';
                            } else if (status === 402) {
                                const h = this._vm.$createElement;
                                Notification({
                                    title: '授权提醒',
                                    duration: 0,
                                    dangerouslyUseHTMLString: true,
                                    message: h('span', null, [
                                        h('span', null, '授权已过期，请重新'),
                                        h('a', {
                                            style: 'color: #3579f8',
                                            attrs: {
                                                href: 'javascript:;'
                                            },
                                            on: {
                                                click: this._actions.authImport
                                            }
                                        }, '导入授权')
                                    ])
                                });
                            }
                        } else {
                            Message({
                                type: 'error',
                                showClose: true,
                                message: '请求失败'
                            });
                        }
                    });
                },
        // 封装一个 ajax 方法---Service
        ajaxService(context, {
            url,
            data,
            method,
            succeed
        }) {
            if (context.state.env === 'production') {
                url;
            } else {
                url;
            }

            axios({
                url: url,
                method: method || 'get',
                params: (method || 'get') == 'get' ? data : {},
                headers: {
                    'token': sessionStorage.token
                },
                data: data || {}
            }).then(function (res) {
                console.log(res)
                if (res.data.code && res.data.code.toString() !== '200') {
                    Message({
                        type: 'warning',
                        showClose: true,
                        message: res.data.message,
                    });
                    return false;
                }

                if (succeed && typeof succeed == 'function') {
                    succeed(res.data);
                }
            }).catch((error) => {
                console.log(error);

                if (error.response) {
                    const status = error.response.status;
                    if (status === 401) {
                        window.location.href = context.state.env === 'production' ? './#/login' : '/#/login';
                    } else if (status === 402) {
                        const h = this._vm.$createElement;
                        Notification({
                            title: '授权提醒',
                            duration: 0,
                            dangerouslyUseHTMLString: true,
                            message: h('span', null, [
                                h('span', null, '授权已过期，请重新'),
                                h('a', {
                                    style: 'color: #3579f8',
                                    attrs: {
                                        href: 'javascript:;'
                                    },
                                    on: {
                                        click: this._actions.authImport
                                    }
                                }, '导入授权')
                            ])
                        });
                    }
                } else {
                    Message({
                        type: 'error',
                        showClose: true,
                        message: '请求失败'
                    });
                }
            });
        },
        authImport() {
            const h = this._vm.$createElement;
            MessageBox({
                title: '导入授权',
                dangerouslyUseHTMLString: true,
                message: h('textarea', {
                    style: 'width: 100%; height: 100px;',
                    attrs: {
                        id: 'authStr'
                    }
                }, ''),
                showCancelButton: true,
                confirmButtonText: '确定',
                cancelButtonText: '取消'
            }).then(() => {
                const authStr = document.getElementById('authStr');
                this.dispatch('ajax', {
                    url: 'api/auther',
                    method: 'post',
                    data: {
                        content: authStr.value
                    },
                    succeed: () => {
                        Message({
                            type: 'success',
                            message: '授权导入成功'
                        });
                        window.location.reload();
                    }
                });
            });
        },
        //传入非结构化数据
        changeUnstructure({
            commit
        }, obj) {
            return commit('ChangeUnStructure', obj)
        }
    },
    mutations: {
        ChangeUnStructure(state, obj) {
            state.unstructureData = obj;
        }
    }
});

export default store;